from neo4j import GraphDatabase

# 连接到Neo4j数据库
uri = "bolt://localhost:7687"
user = "neo4j"
password = "password"

# 创建驱动实例
driver = GraphDatabase.driver(uri, auth=(user, password))

def check_dragonfly_relations_detailed():
    with driver.session() as session:
        # 获取"Dragonfly"的所有出发关系及其原始关系名称
        outgoing_query = """
        MATCH (n:Entity {name: 'Dragonfly'})-[r]->(target)
        RETURN type(r) as relationshipType, r.original_relation as originalRelation, target.name as targetName
        ORDER BY relationshipType, targetName
        """
        outgoing_result = session.run(outgoing_query)
        
        print("=== Dragonfly的所有出发关系 ===")
        for record in outgoing_result:
            rel_type = record["relationshipType"]
            original_rel = record["originalRelation"]
            target = record["targetName"]
            print(f"关系类型: {rel_type}, 原始关系: {original_rel}, 目标实体: {target}")
        
        # 获取指向"Dragonfly"的所有关系及其原始关系名称
        incoming_query = """
        MATCH (source)-[r]->(n:Entity {name: 'Dragonfly'})
        RETURN type(r) as relationshipType, r.original_relation as originalRelation, source.name as sourceName
        ORDER BY relationshipType, sourceName
        """
        incoming_result = session.run(incoming_query)
        
        print("\n=== 指向Dragonfly的所有关系 ===")
        for record in incoming_result:
            rel_type = record["relationshipType"]
            original_rel = record["originalRelation"]
            source = record["sourceName"]
            print(f"关系类型: {rel_type}, 原始关系: {original_rel}, 来源实体: {source}")
        
        # 统计每种关系类型的数量
        count_query = """
        MATCH (n:Entity {name: 'Dragonfly'})-[r]->(target)
        RETURN type(r) as relationshipType, count(r) as count
        ORDER BY count DESC
        """
        count_result = session.run(count_query)
        
        print("\n=== Dragonfly出发关系类型统计 ===")
        for record in count_result:
            rel_type = record["relationshipType"]
            count = record["count"]
            print(f"{rel_type}: {count}个关系")
        
        # 获取Dragonfly节点的所有属性
        node_properties_query = """
        MATCH (n:Entity {name: 'Dragonfly'})
        RETURN n
        """
        node_result = session.run(node_properties_query)
        
        print("\n=== Dragonfly节点属性 ===")
        for record in node_result:
            node = record["n"]
            print("节点属性:")
            for key, value in node.items():
                print(f"  {key}: {value}")

# 执行查询并关闭连接
check_dragonfly_relations_detailed()
driver.close() 